﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class WebUserControl_Baocaotoncuoi : System.Web.UI.UserControl
{
    BCDataCenter.DataAccessLayer.DAL dt;
    string error = "";
    static DataSet ds = new DataSet();
    static string location = "";
    static string closedate = "";
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        try
        {
            if (!IsPostBack)
            {
                ds = dt.MyExecuteQuery(ref error, "P_select_location", CommandType.StoredProcedure, new SqlParameter("@sitecode", Session["Sitecode"].ToString()));
                location = ds.Tables[0].Rows[0][0].ToString();
                Label1.Text = "Trung Tâm Phân phối : " + ds.Tables[0].Rows[0][1].ToString();
                Label2.Text = "   ; " + ds.Tables[0].Rows[0][2].ToString();
                closedate = ds.Tables[0].Rows[0]["closedate"].ToString();
                TextBox1.Text = closedate;
                ds = dt.MyExecuteQuery(ref error, "P_Get_Qty", CommandType.StoredProcedure, new SqlParameter("@location", location));
            }
            else
            {
                if (flag == true)
                {
                    if (ds.Tables[0].Rows.Count >= 1)
                    {
                        Gv_Qty.DataSource = ds.Tables[0];
                        Gv_Qty.DataBind();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
        //ds.Dispose();
    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = "Bao_Cao_ton_cuoi";
            ASPxGridViewExporter1.WriteXlsToResponse(fileName);
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected DataTable DTable()
    {
        DataTable dts = new DataTable();
        dts.Columns.Add("STT");
        dts.Columns.Add("itemkey");
        dts.Columns.Add("description");
        dts.Columns.Add("Qty");
        return dts;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        dt = new BCDataCenter.DataAccessLayer.DAL();
        if (TextBox1.Text != closedate)
        {
            string table = "DIN" + TextBox1.Text.Substring(3, 2) + TextBox1.Text.Substring(6);
            string date =TextBox1.Text.ToString();
            if (CheckBox1.Checked)
            {
                ds = dt.MyExecuteQuery(ref error, "GET_QTY_DINyymm_0", CommandType.StoredProcedure, new SqlParameter("@TableName", table), new SqlParameter("@location",location),new SqlParameter("@date",date));

            }
            else
            {
                ds = dt.MyExecuteQuery(ref error, "GET_QTY_DINyymm", CommandType.StoredProcedure, new SqlParameter("@TableName", table), new SqlParameter("@location", location),new SqlParameter("@date",date));
                //ds = dt.MyExecuteQuery@location= new SqlDataAdapter("GET_QTY_DINyymm", con);
            }
            
        }
        else
        {
            if (CheckBox1.Checked)
            {

                ds = dt.MyExecuteQuery(ref error, "P_Get_Qty", CommandType.StoredProcedure, new SqlParameter("@location", location));
            }
            else
            {
                ds = dt.MyExecuteQuery(ref error, "P_Get_Qty_qtyonhand", CommandType.StoredProcedure, new SqlParameter("@location", location));
                //da = new SqlDataAdapter("P_Get_Qty_qtyonhand", con);
            }
        }
        
       
        //----------------------------------------
        
       // ds = dt.MyExecuteQuery(ref error, "P_Get_Qty", CommandType.StoredProcedure, new SqlParameter("@location", location));
        try
        {
            if (ds.Tables[0].Rows.Count >= 1)
            {
                Gv_Qty.DataSource = ds.Tables[0];
                Gv_Qty.DataBind();
                flag = true;
            }
        }
        catch
        {
            Gv_Qty.DataSource = DTable();
            Gv_Qty.DataBind();
            uscMsgBox1.AddMessage("Không có dữ liệu trong thời gian đâ chọn", uscMsgBox.enmMessageType.Error);
        }
    }
}
